﻿using static _01_Tower3._0.Map;
namespace _01_Tower3._0
{
    class Hero : Character
    {
        Dir dir;
        Dir dir2;
        Map map;
        static public Pos pos;
        int monNum, tooNum;

        int damage;
        Random rand;

        CombatSystem cs;
        ToolMenu toolMenu;
        DialogSystem ds;

        public bool canUseKey;
        public bool canUseKey0;
        public bool canUseKey1;
        public bool doorIsOpen1;
        public bool doorIsOpen2;
        bool trapIsOpen;
        bool trapClear;
        int trapY;
        int trapClearY;
        public int unLock;
        public static string csTarget;

        public struct Pos()
        {
            public int x;
            public int y;
            public int x0;
            public int y0;
        }

        enum Dir
        {
            None,
            Up,
            Down,
            Left,
            Right,
        };

        public void AddDef()
        {
            Def++;
        }
        public void AddHp()
        {
            Hp += 2;
        }

        public void AddAtk()
        {
            Atk++;
        }

        //public int deff;
        public Hero(string name, int hp, int mp, int atk, int def, Map map) : base(name, hp, mp, atk, def, map)
        {
            //deff = def;
            this.map = map;
        }

        public void Init(Map map, CombatSystem cs, ToolMenu toolMenu,DialogSystem ds)
        {
            this.ds = ds;
            this.cs = cs;
            this.toolMenu = toolMenu;
            pos.x0 = pos.x;
            pos.y0 = pos.y;
            monNum = tooNum = 1;
            rand = new Random();
            canUseKey = false;
            canUseKey0 = false;
            canUseKey1 = false;
            doorIsOpen1 = false;
            doorIsOpen2 = false;
            trapIsOpen = false;
            trapY = 1;
            trapClearY = 1;
            unLock = 0;
            csTarget = " ";
            dir = dir2 = Dir.None;
        }
        void Move()
        {
            //Utility.MyPrint(" ", pos.x0, pos.y0);
            switch (dir)
            {
                case Dir.Up:
                    pos.y--;
                    break;
                case Dir.Down:
                    pos.y++;
                    break;
                case Dir.Left:
                    pos.x--;
                    break;
                case Dir.Right:
                    pos.x++;
                    break;
            }
        }
        public void Input()
        {
            dir = Dir.None;
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.UpArrow) || InputSystem.Instance.IsKeyPressed(ConsoleKey.W))
            {
                dir = Dir.Up;
                dir2 = Dir.Up;
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.DownArrow) || InputSystem.Instance.IsKeyPressed(ConsoleKey.S))
            {
                dir = Dir.Down;
                dir2 = Dir.Down;
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.LeftArrow) || InputSystem.Instance.IsKeyPressed(ConsoleKey.A))
            {
                dir = Dir.Left;
                dir2 = Dir.Left;
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.RightArrow) || InputSystem.Instance.IsKeyPressed(ConsoleKey.D))
            {
                dir = Dir.Right;
                dir2 = Dir.Right;
            }
        }
        Pos GetNextPos()
        {
            Pos next;
            next.x = next.y = 0;
            next.x0 = next.y0 = 0;
            switch (dir)
            {
                case Dir.Up:
                    next.x = pos.x;
                    next.y = pos.y - 1;
                    break;
                case Dir.Down:
                    next.x = pos.x;
                    next.y = pos.y + 1;
                    break;
                case Dir.Left:
                    next.x = pos.x - 1;
                    next.y = pos.y;
                    break;
                case Dir.Right:
                    next.x = pos.x + 1;
                    next.y = pos.y;
                    break;
            }
            return next;
        }
        int GetMapType(Pos pos)
        {
            return (int)map.data[map.currentIndex, pos.y * 60 + pos.x].type;
        }
        public void Update()
        {
            if (GetMapType(GetNextPos()) == (int)Map.Type.None)// 前方空气就移动
            {
                Move();
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.Tool)// 前方药物就移动并拾取
            {
                Move();
                InfoSystem.Instance.AddInfo($"获得药物", ConsoleColor.Red);
                toolMenu.AddInfo($"药物", ConsoleColor.Red);
                map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.Map1Key0)// 前方钥匙就移动并拾取
            {
                Move();
                InfoSystem.Instance.AddInfo($"获得钥匙1.0", ConsoleColor.Yellow);
                
                toolMenu.AddInfo($"钥匙1.0", ConsoleColor.Yellow);
                map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.Map2Key0)// 前方钥匙就移动并拾取
            {
                Move();
                InfoSystem.Instance.AddInfo($"获得钥匙2.0", ConsoleColor.Yellow);
                
                toolMenu.AddInfo($"钥匙2.0", ConsoleColor.Yellow);
                map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.Map2Key1)// 前方钥匙就移动并拾取
            {
                Move();
                InfoSystem.Instance.AddInfo($"获得钥匙2.1", ConsoleColor.Yellow);
                
                toolMenu.AddInfo($"钥匙2.1", ConsoleColor.Yellow);
                map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.Map2Button0)// 按钮
            {
                Move();
                InfoSystem.Instance.AddInfo($"好像听到什么动静。。", ConsoleColor.Red);
                trapIsOpen = !trapIsOpen;
                map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.TrapWall)// 陷阱墙
            {
                Environment.Exit(0);
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.Map1Step0)// 地图一前往地图二 阶梯 钥匙 开门
            {
                canUseKey = true;
                if (doorIsOpen1)
                {
                    Move();
                    map.ChangeMap(1);
                }
                dir = Dir.None;
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.Map2Step0)// 地图二回到地图一 阶梯
            {
                Move();
                map.ChangeMap(0);
                dir = Dir.None;
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.Map2Step1)// 地图二前往地图三 阶梯 钥匙 开门
            {
                canUseKey0 = true;
                canUseKey1 = true;
                if (doorIsOpen2)
                {
                    Move();
                    map.ChangeMap(2);
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.King)
            {
                ds.StartDialog(0);
                dir = Dir.None;
            }
            #region 史莱姆
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonSlime)// 前方史莱姆就进入战斗
            {
                if (SceneMap1.slime[0].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.slime[0].Hp > 0)
                {
                    csTarget = "史莱姆";
                    cs.StartCombat(SceneManger.hero, SceneMap1.slime[0]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonSlime2)// 前方史莱姆就进入战斗
            {
                if (SceneMap1.slime[1].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.slime[1].Hp > 0)
                {
                    csTarget = "史莱姆";
                    cs.StartCombat(SceneManger.hero, SceneMap1.slime[1]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonSlime3)// 前方史莱姆就进入战斗
            {
                if (SceneMap1.slime[2].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.slime[2].Hp > 0)
                {
                    csTarget = "史莱姆";
                    cs.StartCombat(SceneManger.hero, SceneMap1.slime[2]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonSlime4)// 前方史莱姆就进入战斗
            {
                if (SceneMap1.slime[3].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.slime[3].Hp > 0)
                {
                    csTarget = "史莱姆";
                    cs.StartCombat(SceneManger.hero, SceneMap1.slime[3]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonSlime5)// 前方史莱姆就进入战斗
            {
                if (SceneMap1.slime[4].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.slime[4].Hp > 0)
                {
                    csTarget = "史莱姆";
                    cs.StartCombat(SceneManger.hero, SceneMap1.slime[4]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonSlime6)// 前方史莱姆就进入战斗
            {
                if (SceneMap1.slime[5].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.slime[5].Hp > 0)
                {
                    csTarget = "史莱姆";
                    cs.StartCombat(SceneManger.hero, SceneMap1.slime[5]);
                    dir = Dir.None;
                }
            }
            #endregion
            #region 哥布林
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonGoblin)// 前方哥布林就进入战斗
            {
                if (SceneMap1.goblin[0].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得哥布林长矛", ConsoleColor.Green);
                    toolMenu.AddInfo($"哥布林长矛", ConsoleColor.Green);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.goblin[0].Hp > 0)
                {
                    csTarget = "哥布林";
                    cs.StartCombat(SceneManger.hero, SceneMap1.goblin[0]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonGoblin2)// 前方哥布林就进入战斗
            {
                if (SceneMap1.goblin[1].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得哥布林长矛", ConsoleColor.Green);
                    toolMenu.AddInfo($"哥布林长矛", ConsoleColor.Green);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.goblin[1].Hp > 0)
                {
                    csTarget = "哥布林";
                    cs.StartCombat(SceneManger.hero, SceneMap1.goblin[1]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonGoblin3)// 前方哥布林就进入战斗
            {
                if (SceneMap1.goblin[2].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得哥布林长矛", ConsoleColor.Green);
                    toolMenu.AddInfo($"哥布林长矛", ConsoleColor.Green);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.goblin[2].Hp > 0)
                {
                    csTarget = "哥布林";
                    cs.StartCombat(SceneManger.hero, SceneMap1.goblin[2]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonGoblin4)// 前方哥布林就进入战斗
            {
                if (SceneMap1.goblin[3].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得哥布林长矛", ConsoleColor.Green);
                    toolMenu.AddInfo($"哥布林长矛", ConsoleColor.Green);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.goblin[3].Hp > 0)
                {
                    csTarget = "哥布林";
                    cs.StartCombat(SceneManger.hero, SceneMap1.goblin[3]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonGoblin5)// 前方哥布林就进入战斗
            {
                if (SceneMap1.goblin[4].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得哥布林长矛", ConsoleColor.Green);
                    toolMenu.AddInfo($"哥布林长矛", ConsoleColor.Green);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.goblin[4].Hp > 0)
                {
                    csTarget = "哥布林";
                    cs.StartCombat(SceneManger.hero, SceneMap1.goblin[4]);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)Map.Type.MonGoblin6)// 前方哥布林就进入战斗
            {
                if (SceneMap1.goblin[5].Hp <= 0)
                {
                    Move();
                    map.data[map.currentIndex, pos.y * 60 + pos.x].type = Map.Type.None;
                    InfoSystem.Instance.AddInfo($"获得哥布林长矛", ConsoleColor.Green);
                    toolMenu.AddInfo($"哥布林长矛", ConsoleColor.Green);
                }
                else if (SceneManger.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (SceneMap1.goblin[5].Hp > 0)
                {
                    csTarget = "哥布林";
                    cs.StartCombat(SceneManger.hero, SceneMap1.goblin[5]);
                    dir = Dir.None;
                }
            }
            #endregion
        }

        public void Escape()
        {
            pos.x--;
            pos.y--;
        }

        public void FixUpdate()
        {
            TrapWallPrint();
        }

        public void Render()
        {
            Console.SetCursorPosition(pos.x0, pos.y0);
            Console.WriteLine(" ");
            pos.x0 = pos.x;
            pos.y0 = pos.y;
            Console.SetCursorPosition(pos.x, pos.y);
            if (dir2 == Dir.Up)
            {
                Console.WriteLine("^");
            }
            else if (dir2 == Dir.Down)
            {
                Console.WriteLine("V");
            }
            else if (dir2 == Dir.Left)
            {
                Console.WriteLine("<");
            }
            else if (dir2 == Dir.Right)
            {
                Console.WriteLine(">");
            }
            
        }

        void TrapWallPrint()
        {
            
            if (trapY < 29 && trapIsOpen)
            {
                map.data[map.currentIndex, trapY * 60 + 58].type = Map.Type.TrapWall;
                map.data[map.currentIndex, trapY * 60 + 57].type = Map.Type.TrapWall;
                map.data[map.currentIndex, trapY * 60 + 56].type = Map.Type.TrapWall;
                map.data[map.currentIndex, trapY * 60 + 55].type = Map.Type.TrapWall;
                map.data[map.currentIndex, trapY * 60 + 54].type = Map.Type.TrapWall;
                map.data[map.currentIndex, trapY * 60 + 53].type = Map.Type.TrapWall;
                trapY++;
                trapClear = true;
            }
            else if(trapClearY < 29 && !trapIsOpen && trapClear)
            {
                map.data[map.currentIndex, trapClearY * 60 + 58].type = Map.Type.None;
                map.data[map.currentIndex, trapClearY * 60 + 57].type = Map.Type.None;
                map.data[map.currentIndex, trapClearY * 60 + 56].type = Map.Type.None;
                map.data[map.currentIndex, trapClearY * 60 + 55].type = Map.Type.None;
                map.data[map.currentIndex, trapClearY * 60 + 54].type = Map.Type.None;
                map.data[map.currentIndex, trapClearY * 60 + 53].type = Map.Type.None;
                Utility.MyPrint(" ", map.data[map.currentIndex, trapClearY * 60 + 58].pos.x, map.data[map.currentIndex, trapClearY * 60 + 58].pos.y);
                Utility.MyPrint(" ", map.data[map.currentIndex, trapClearY * 60 + 57].pos.x, map.data[map.currentIndex, trapClearY * 60 + 57].pos.y);
                Utility.MyPrint(" ", map.data[map.currentIndex, trapClearY * 60 + 56].pos.x, map.data[map.currentIndex, trapClearY * 60 + 56].pos.y);
                Utility.MyPrint(" ", map.data[map.currentIndex, trapClearY * 60 + 55].pos.x, map.data[map.currentIndex, trapClearY * 60 + 55].pos.y);
                Utility.MyPrint(" ", map.data[map.currentIndex, trapClearY * 60 + 54].pos.x, map.data[map.currentIndex, trapClearY * 60 + 54].pos.y);
                Utility.MyPrint(" ", map.data[map.currentIndex, trapClearY * 60 + 53].pos.x, map.data[map.currentIndex, trapClearY * 60 + 53].pos.y);
                trapClearY++;
            }
        }

        public void End()
        {

        }
    }
}
